package com.token.iot.bratterycharging.boss.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.outstanding.framework.base.TransactionLogger;
import com.outstanding.framework.container.springmvc.WebContext;
import com.outstanding.framework.core.PendingException;
import com.token.iot.bratterycharging.boss.service.OrderServcice;

@Controller
@RequestMapping("/order")
public class OrderController {

	@Autowired
	private OrderServcice orderService;

	/**
	 * 订单查询
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/list.do")
	public PageInfo orderList(@RequestBody ReqPageDto dto) throws PendingException {
		TransactionLogger.debug("staring order list " + String.valueOf(dto == null) + " ");
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		if (operid == null) {
			String agentid = (String) WebContext.getSession().getAttribute("_agentid");
			if (agentid == null) {
				String mid = (String) WebContext.getSession().getAttribute("_merchantid");
				if (mid == null) {
					throw new PendingException(ResCode.OperNotLogin);
				} else {
					// merid = mid;
					return orderService.queryByMerchantId(dto.getPageNo(), dto.getPageSize(), mid);
				}
			} else {
				return orderService.queryByAgentId(dto.getPageNo(), dto.getPageSize(), agentid);
			}
		} else {
			return orderService.query(dto.getPageNo(), dto.getPageSize());
		}
	}

	/**
	 * 订单查询
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/query.do")
	public PageInfo query(@RequestBody ReqPageDto dto) throws PendingException {
		TransactionLogger.debug("staring order list " + String.valueOf(dto == null) + " ");
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		if (operid == null) {
			String agentid = (String) WebContext.getSession().getAttribute("_agentid");
			if (agentid == null) {
				String mid = (String) WebContext.getSession().getAttribute("_merchantid");
				if (mid == null) {
					throw new PendingException(ResCode.OperNotLogin);
				}
			}
		}
		return orderService.queryByReqPageDto(dto.getPageNo(), dto.getPageSize(),dto);

	}

	/**
	 * 订单
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/count.do")
	public Amount count() throws PendingException {
		TransactionLogger.debug("staring order count  ");
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		if (operid == null) {
			String agentid = (String) WebContext.getSession().getAttribute("_agentid");
			if (agentid == null) {
				String mid = (String) WebContext.getSession().getAttribute("_merchantid");
				if (mid == null) {
					throw new PendingException(ResCode.OperNotLogin);
				} else {
					// merid = mid;
					return orderService.countByMerchantId(mid);
				}
			} else {
				return orderService.countByAgentId(agentid);
			}
		} else {
			return orderService.countAll();
		}
	}

}
